package id.net.iconpln.bb.client.view;

import id.net.iconpln.bb.client.AppEvents;
import id.net.iconpln.bb.client.model.NavigationModel;
import id.net.iconpln.bb.client.model.SessionVariabel;
import id.net.iconpln.bb.client.service.BatubaraService;
import id.net.iconpln.bb.client.service.BatubaraServiceAsync;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;

public class AppView extends View {
	private final BatubaraServiceAsync pbService = GWT.create(BatubaraService.class);
	public String u;
	private String n;
	private String upj;
	private String npj;
	public static final String CENTER_PANEL = "centerPanel";
	public static final String WEST_PANEL = "westPanel";
	public static final String NORTH_PANEL = "northPanel";
	public static final String VIEWPORT = "viewPort";
	SessionVariabel s;
	private Viewport viewport;
	private ContentPanel centerPanel;
	private HtmlContainer northPanel;
	private ContentPanel westPanel;
	private NavigationModel model;
	private Timer t;
	public AppView(Controller controller) {		
		super(controller);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		}
	}

	private void initUI() {
		model = new NavigationModel();

		viewport = new Viewport();
		viewport.setLayout(new BorderLayout());
		Registry.register(VIEWPORT, viewport);

		createNorth();
		createWest();
		createCenter();

		viewport.syncSize();
		RootPanel.get().add(viewport);
	}

	private void createCenter() {
		centerPanel = new ContentPanel();
		centerPanel.setBorders(false);
		centerPanel.setHeaderVisible(false);
		centerPanel.setLayout(new FitLayout());

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
		data.setMargins(new Margins(5, 5, 5, 0));
		viewport.add(centerPanel, data);
		Registry.register(CENTER_PANEL, centerPanel);
	}

	private void createWest() {
		BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 220,
				150, 320);
		data.setMargins(new Margins(5, 5, 5, 5));
		data.setCollapsible(true);	

		westPanel = new ContentPanel();

//		ToolBar toolBar = new ToolBar();
//
//		ButtonGroup group = new ButtonGroup(2);
//		// group.setHeaderVisible(false);
//		// Button btn = new Button("Cool", IconHelper.createStyle("add16"));
//		// group.add(btn);
//		
//		final UnitupiSelector selectorUpi = new UnitupiSelector();
//		final UnitapSelector selector = new UnitapSelector();
//		final UnitupSelector selectorUpj = new UnitupSelector();
//		
//		Button upi = new Button("UPI : ");
//		upi.setIcon(IconHelper.createStyle("icon-pln"));
//		upi.addSelectionListener(new SelectionListener<ButtonEvent>() {
//			public void componentSelected(ButtonEvent ce) {
////				pbService.setToAllApj(new AsyncCallback<String>() {
////							public void onFailure(Throwable caught) {
////								GWT.log(caught.getMessage(), null);
////							}
////							public void onSuccess(String result) {
////								GWT.log("who am i : "+ result,null);								
////							}
////						});
//				
//				
//				selectorUpi.setRawValue("");
//				selectorUpi.setEmptyText("Select UPI...");
//				
//				selector.setRawValue("");
//				selector.setEmptyText("Select APJ...");
//				selector.getStore().removeAll();
//				
//				selectorUpj.setRawValue("");
//				selectorUpj.getStore().removeAll();
//				selectorUpj.setEmptyText("Select UPJ...");
//				
//				
//				
//				
//			}
//		});
//		
//		group.add(upi);
//		
//		selectorUpi.addListener(Events.Select, new Listener<FieldEvent>() {
//			public void handleEvent(FieldEvent be) {
////				GWT.log("unitupi :" + u, null);
//				Registry.register("unitupiRegister", selectorUpi.getValue().getUnitupi());
//				Registry.register("unitupiNameRegister", selectorUpi.getRawValue().toString());
//
//				Registry.register("unitapRegister", null);
//				Registry.register("unitapNameRegister", null);
//				
//				Registry.register("unitupRegister", null);
//				Registry.register("unitupNameRegister", null);
//
//				Registry.register("isUnit", "upi");
////				UnitupBean vv = selectorUpj.getValue();
////				GWT.log(v.getUnitup() + "Selector " + v.getNama(), null);
////				selectorUpj.getStore().removeAll();
////				selectorUpj.getStore().getLoader().load();
////				upj = vv.getUnitup();
////				npj = vv.getNama();
//				String unitupis = selectorUpi.getValue().getUnitupi().toString();
//				pbService.setUnitupiS(unitupis, new AsyncCallback<String>() {
//					public void onFailure(Throwable caught) {
//						GWT.log(caught.getMessage(), null);
//					}
//					@Override
//					public void onSuccess(String result) {
//						// TODO Auto-generated method stub
//						GWT.log("Ok " + result, null);
//					}
//				});
//
//				selector.getStore().removeAll();
//				selector.getStore().getLoader().load();
//				selector.setRawValue("");
//				selector.setEmptyText("Select APJ...");
//				
//				selectorUpj.getStore().removeAll();
//				selectorUpj.setRawValue("");
//				selectorUpj.setEmptyText("Select UPJ...");
//			}
//		});
//		
//		group.add(selectorUpi);
//		toolBar.add(group);
//		
//		Button aplL = new Button("APJ : ");
//		aplL.setIcon(IconHelper.createStyle("icon-photos"));
//		aplL.addSelectionListener(new SelectionListener<ButtonEvent>() {
//			public void componentSelected(ButtonEvent ce) {
//				pbService.setToAllApj(new AsyncCallback<String>() {
//							public void onFailure(Throwable caught) {
//								GWT.log(caught.getMessage(), null);
//							}
//							public void onSuccess(String result) {
//								GWT.log("who am i : "+ result,null);								
//							}
//						});
//				
//				selector.setRawValue("");
//				selector.setEmptyText("Select APJ...");
//				selectorUpj.setRawValue("");
//				selectorUpj.setEmptyText("Select UPJ...");
//			}
//		});
//
//		group.add(aplL);
//		
//		selector.addListener(Events.Select, new Listener<FieldEvent>() {
//			public void handleEvent(FieldEvent be) {				
//				Registry.register("unitapRegister", selector.getValue().getUnitap());
//				Registry.register("unitapNameRegister", selector.getRawValue().toString());
//				
//				Registry.register("unitupRegister", null);
//				Registry.register("unitupNameRegister", null);
//				Registry.register("isUnit", "ap");
//				UnitapBean v = selector.getValue();
//				u = v.getUnitap();
//				n = v.getNama();
////				SessionVariabel x = new SessionVariabel();
//						pbService.setUnitapSession(u, new AsyncCallback<String>() {
//							public void onFailure(Throwable caught) {
//							}
//							public void onSuccess(String result) {
//								GWT.log("Set Session ap: " + result, null);
//							}
//						});
////				x.setUnitap(v.getUnitap());
//				GWT.log("unitupi :" + Registry.get("unitupiRegister"), null);
//				GWT.log("kode : " + u + " nama apj :" + n, null);
//				selectorUpj.getStore().removeAll();
//				selectorUpj.getStore().getLoader().load();
//				selectorUpj.setRawValue("");
//				selectorUpj.setEmptyText("Select UPJ...");
//			
//			}
//		});
//
//		group.add(selector);
//		toolBar.add(group);
//
//		// ToolBar toolBarUpj = new ToolBar();
//		// group = new ButtonGroup(2);
//
//		Button uplL = new Button("UPJ : ");
//		uplL.setIcon(IconHelper.createStyle("icon-house"));
//		group.add(uplL);
//
//
//		selectorUpj.addListener(Events.Select, new Listener<FieldEvent>() {
//
//			public void handleEvent(FieldEvent be) {
//				Registry.register("unitupRegister", selectorUpj.getValue().getUnitup());
//				Registry.register("unitupNameRegister", selectorUpj.getRawValue().toString());
//				Registry.register("isUnit", "up");
//				GWT.log("unitup :" + u, null);
//				UnitupBean vv = selectorUpj.getValue();
////				GWT.log(v.getUnitup() + "Selector " + v.getNama(), null);
////				selectorUpj.getStore().removeAll();
////				selectorUpj.getStore().getLoader().load();
//				upj = vv.getUnitup();
//				npj = vv.getNama();
//				pbService.setUnitupSession(upj, new AsyncCallback<String>() {
//					public void onFailure(Throwable caught) {
//					}
//					public void onSuccess(String result) {
//						GWT.log("Set Session up: " + result, null);
//					}
//				});
//			}
//		});
		
//		group.add(selectorUpj);
//		toolBar.add(group);

//		westPanel.setTopComponent(toolBar);

		viewport.add(westPanel, data);
		Registry.register(WEST_PANEL, westPanel);
	}

	private void createNorth() {
		StringBuffer sb = new StringBuffer();		
		
		sb.append("<div id='demo-theme' style='color:white;'></div><div id='demo-title' style='color:white;'>Batubara Management System - "+ Registry.get("isModule") +" (" + Registry.get("namaUnit") + ") - " + Registry.get("whoAmI") + "</div>");
		northPanel = new HtmlContainer(sb.toString());
		// northPanel.setEnableState(false);
		northPanel.setId("demo-header");
		northPanel.addStyleName("x-small-editor");

	    Button logout = new Button("Logout");
//	    login.disable();
	    logout.addSelectionListener(new SelectionListener<ButtonEvent>() {
	      public void componentSelected(ButtonEvent ce) {
	    	  MessageBox.confirm("Logout","Service logout", 
	    	  new Listener<MessageBoxEvent>() {  
			       public void handleEvent(MessageBoxEvent ce) {
			    	   
			    	   Button btn = ce.getButtonClicked();//
//			    	   Registry.register("isModule", words[1]);
			    	   GWT.log(btn.getText(), null);
			    	   if (btn.getText().equals("Yes")){	  
	    	  
			    		   	pbService.logout(new AsyncCallback<String>() {
			    		   			public void onFailure(Throwable caught) {
		  				// Show the RPC error message to the user
			    		   			GWT.log("error",null);
			    		   	}
			    		   	public void onSuccess(String result) {	
		  				
			    		   		GWT.log("my ip :" + result, null);
			    		   		redirect(GWT.getHostPageBaseURL() + "BatubaraOnline.html");
			    		   		return;
		  			}
	          });
			    	   }else{
			    		   return ;
			    	   }
			       }

				
		});
	      }
	    });

	    
//		Hyperlink lg = new Hyperlink("Logout", "");
//		lg.setTitle("Logout");
//		lg.setStyleName("color:white;");
//		lg.addClickListener(new ClickListener() {
//			@Override
//			public void onClick(Widget sender) {
//				// TODO Auto-generated method stub
//				redirect(GWT.getHostPageBaseURL() + "BatubaraOnline.html");
//			}
//		});

		northPanel.add(logout, "#demo-theme");

		BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 33);
		data.setMargins(new Margins());
		viewport.add(northPanel, data);
		Registry.register(NORTH_PANEL, northPanel);
	}

	native void redirect(String url)
	/*-{
		$wnd.location.replace(url);
	}-*/;
	
//	 private void loadfrom(){
//		    GWT.log("Load timer ", null);
//		    model = (NavigationModel) Registry.get(Main.MODEL);
//		    Main.showPage(model.findEntry("test"));
//	 }
}
